﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.IO;
using System.Windows.Media.Imaging;

namespace TravelAgencySilverlight.Views.Converters
{
    public class ByteArrayToImageSource : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            byte[] bytes = value as byte[];

            if (bytes == null)
            {
                return null;
            }                        

            MemoryStream photoContentStream = new MemoryStream(bytes);
            BitmapImage bitmapImage = new BitmapImage();

            
        
            try
            {
                //bitmapImage.BeginInit();
                //bitmapImage.StreamSource = photoContentStream;
                //bitmapImage.EndInit();

                bitmapImage.SetSource(photoContentStream);
                
            }
            catch (Exception ex)
            {
                return null;
            }

            return bitmapImage;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
